#include "studentlist.h"
#include <unistd.h>
studentList::studentList()
{

}
studentList::~studentList(){

}
void studentList::init(){
    ReadFile(); //读取data文件
}
//主菜单
void studentList::menu(){
    string sel = "0";
    system("clear");
    cout  << "\t****************学生信息管理系统V114.514****************" << endl;
    cout  << "\t输入数字编号选择操作类型：" << endl;
    cout  << "\t1 查看学生信息" << endl;
    cout  << "\t2 添加学生信息" << endl;
    cout  << "\t3 按学号查找学生信息" << endl;
    cout  << "\t4 按学号删除学生信息" << endl;
    cout  << "\t5 按学号更新学生信息" << endl;
    cout  << "\t0 退出" << endl;
    cout  << "\t请选择【0~5】：" ;
    cin >> sel;
    while(sel != "0" && sel != "1" && sel != "2" && sel != "3" && sel != "4" && sel != "5")
    {
        cout << "\t输入无效，请重新输入【0~5】：";
        cin >>  sel;
    }
    if(sel == "0"){
        exit(0);
    }
    else if(sel == "1"){
        this->displayList();
        this->menu();
    }
    else if(sel == "2"){
        this->insertList();
        this->menu();
    }
    else if(sel == "3"){
        this->selectList();
        this->menu();
    }
    else if(sel == "4"){
        this->deleteList();
        this->menu();
    }
    else if(sel == "5"){
        this->updateList();
        this->menu();
    }

}
//读取文件
void studentList::ReadFile(){
    ifstream ifs;
    ifs.open("student.dat",ios::in);
    int n = 0;
    ifs >> n;
    for(int  i =0;i<n;i++){
        student s;
        ifs >>  s.num >> s.name >> s.age >> s.gender >> s.addr;
        stuList.push_back(s);
    }
    ifs.close();
}
//写入文件
void studentList:: WriteFile(){
    ofstream ofs;
    ofs.open("student.dat",ios::out);
    ofs << stuList.size() << endl;
    for(int i =0;i< stuList.size();i++){
        ofs << stuList[i].num << " " << stuList[i].name << " " << stuList[i].age << " " << stuList[i].gender << " " << stuList[i].addr << endl;

    }
    ofs.close();

}

void studentList:: insertList(){
    cout <<  "\t********************************************************" << endl;
    cout << "\t学号\t：";

    bool check = false;
    student s;
    do
    {
        check = false;
        cin >> s.num;
        for(int ii = 0;ii<stuList.size();ii++){
            if(s.num  == stuList[ii].num){
                cout << "学号重复，请重新输入：";
                check = true;
                break;
            }
        }
    }
    while(check);
    cout << "\t姓名：";
    cin >> s.name;
    cout << "\t年龄：";
    cin >> s.age;
    cout << "\t性别(f(female),m(male))：";
    cin >> s.gender;
    cout << "\t住址：";
    cin >> s.addr;

    stuList.push_back(s);
    WriteFile();
    cout <<  "\t添加成功" << endl;
    cout << "按回车继续...";
    cin.ignore();
    cin.get();

}

void studentList:: displayList(){
    cout <<  "\t********************************************************" << endl;
    cout << "\t";
    student::showHeader();
    cout <<  "\t********************************************************" << endl;
    for(int ii = 0;ii<stuList.size();ii++){
        cout << "\t"<<setw(4)<<ii;
        stuList[ii].showInfo();
    }
    cout <<  "\t********************************************************" << endl;
    cout << "按回车继续...";
    cin.ignore();
    cin.get();
}
void studentList::deleteList(){
    string keynum;
    bool check = false;
    int ii = 0; //索引
    if(stuList.size()<1){
        cout << "数据库为空！" << endl;
        cout << "按回车继续...";
        cin.ignore();
        cin.get();
        return;
    }
    do{
        check = false;
        cout << "\t请输入删除信息学生的学号：";
        cin >>  keynum;
        cout << "\t查询结果如下："<<endl;

        for(ii=0;ii<stuList.size();ii++)
        {
            if(stuList[ii].num == keynum){
                check =  true;
                cout <<  "\t********************************************************" << endl;
                cout << "\t";
                student::showHeader();
                cout <<  "\t********************************************************" << endl;
                cout << "\t";
                stuList[ii].showInfo();
                break;
            }
        }
        if(!check) {
            cout <<  "\t查无此人！" << endl;
        }
        else{

        }

    cout <<  "\t********************************************************" << endl;
    }
    while(!check);
    cout << "\t是否删除？[Y/n]" << endl;
    string yon;
    cin >> yon;
    if(yon=="no"||yon=="No"||yon=="NO"||yon=="N"||yon=="n"){
        cout << "\t未删除"<<endl;
    }
    else {
        //for(;ii<stuList.size()-1;ii++){
        //    stuList[ii] = stuList[ii+1];
        //}
        stuList.erase(stuList.begin()+ii);
        WriteFile();
        cout << "\t已删除"<<endl;
    }
    cout <<  "\t********************************************************" << endl;
    cout << "按回车继续...";
    cin.ignore();
    cin.get();
}
void studentList::selectList(){
    string keynum;
    bool check = false;
    cout << "\t请输入查询学生的序号：";
    cin >>  keynum;
    cout << "\t查询结果如下："<<endl;

    for(int ii=0;ii<stuList.size();ii++)
    {
        if(stuList[ii].num == keynum){
            check =  true;
            cout <<  "\t********************************************************" << endl;
            cout << "\t";
            student::showHeader();
            cout <<  "\t********************************************************" << endl;
            cout << "\t";
            stuList[ii].showInfo();

            break;
        }
    }
    if(!check) cout <<  "\t查无此人！" << endl;

    cout <<  "\t********************************************************" << endl;
    cout << "按回车继续...";
    cin.ignore();
    cin.get();


}

void studentList::updateList(){
    string keynum;
    bool check = false;
    cout <<  "\t********************************************************" << endl;
    cout << "\t学号\t：";

    int ii = 0; //索引
    if(stuList.size()<1){
        cout << "数据库为空！" << endl;
        cout << "按回车继续...";
        cin.ignore();
        cin.get();
        return;
    }
    student s;
    do
    {
        check = false;
        cin >> s.num;
        for(ii = 0;ii<stuList.size();ii++){
            if(s.num  == stuList[ii].num){
                check = true;
                cout <<  "\t********************************************************" << endl;
                cout << "\t";
                student::showHeader();
                cout <<  "\t********************************************************" << endl;
                cout << "\t";
                stuList[ii].showInfo();
                break;
            }
        }
    }
    while(!check);
    cout << "\t姓名：";
    cin >> s.name;
    cout << "\t年龄：";
    cin >> s.age;
    cout << "\t性别(f(female),m(male))：";
    cin >> s.gender;
    cout << "\t住址：";
    cin >> s.addr;

    stuList[ii]=s;
    WriteFile();
    cout <<  "\t修改成功" << endl;
    cout << "按回车继续...";
    cin.ignore();
    cin.get();
}
